# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : refresh_view.py
# Time       ：2023/8/26 23:00
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    刷新token
"""
from flask import request
from flask_restx import fields

from apis.token import token_ns
from public.api_utils.api_decorator import handle_api_error
from public.api_utils.jwt_utils import JWTUtils
from public.base_response import generate_response, MarshalModel
from public.base_view import AbstractView

refresh_token = {'token': fields.String()}
refresh_token_marshal = MarshalModel('refresh_token', token_ns)


class RefreshView(AbstractView):
    url = '/refresh'

    def __init__(self, *args, **kwargs):
        super(RefreshView, self).__init__(*args, **kwargs)
        self.token = request.json.get('token')

    @handle_api_error()
    @token_ns.expect(refresh_token_marshal.detail_request(refresh_token))
    def post(self):
        """刷新token"""
        token = JWTUtils().refresh_token(self.token)
        return generate_response(data={'token': token})
